package com.wrench.elevator.jedis;

import java.util.Set;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.wrench.elevator.base.user.service.ChengtiService;
import com.wrench.elevator.constant.Dict;
import com.wrench.elevator.exception.WrenchException;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisPubSub;

@Component
public class KeyExpiredListener extends JedisPubSub
{
    @Resource
    private ChengtiService chengtiService;
    
    @Override
    public void unsubscribe()
    {
        super.unsubscribe();
    }
    
    @Override
    public void unsubscribe(String... channels)
    {
        super.unsubscribe(channels);
    }
    
    @Override
    public void subscribe(String... channels)
    {
        super.subscribe(channels);
    }
    
    @Override
    public void psubscribe(String... patterns)
    {
        super.psubscribe(patterns);
    }
    
    @Override
    public void punsubscribe()
    {
        super.punsubscribe();
    }
    
    @Override
    public void punsubscribe(String... patterns)
    {
        super.punsubscribe(patterns);
    }
    
    @Override
    public void onMessage(String channel, String message)
    {
        
    }
    
    @Override
    public void onPMessage(String pattern, String channel, String message)
    {
        if(message.contains("_BACK")) return;
        
        Jedis jedis = RedisUtil.getJedis();
        // System.out.println(jedis.get(message + "_BACK"));
        Set<String> face_id_identitys = jedis.smembers(message + "_BACK");
       
        try
        {
            if (Dict.INTERFACE.equals("YUN"))
                chengtiService.addChengtiCloud(face_id_identitys);
            else
                
                chengtiService.addChengti(face_id_identitys);
        }
        catch (WrenchException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    
    @Override
    public void onSubscribe(String channel, int subscribedChannels)
    {
        // System.out.println("channel:" + channel + "is been subscribed:" + subscribedChannels);
    }
    
    @Override
    public void onPUnsubscribe(String pattern, int subscribedChannels)
    {
        System.out.println("pattern:" + pattern + "is been subscribed:" + subscribedChannels);
    }
    
    @Override
    public void onPSubscribe(String pattern, int subscribedChannels)
    {
        System.out.println("pattern:" + pattern + "is been unsubscribed:" + subscribedChannels);
    }
    
    @Override
    public void onUnsubscribe(String channel, int subscribedChannels)
    {
        
    }
    
    @PostConstruct
    public void init()
    {
        KeyExpiredListener listener = this;
        new Thread(new Runnable()
        {
            
            @Override
            public void run()
            {
                Jedis jedis = RedisUtil.getJedis();
                jedis.psubscribe(listener, "__keyevent@0__:expired");
                
            }
        }).start();
        ;
        
    }
    
}
